//
//  UIImage+Extension.swift
//  weibo
//
//  Created by 王旭 on 16/4/26.
//  Copyright © 2016年 王旭. All rights reserved.
//

import UIKit

extension UIImage {
    //缩放到指定宽度
   func scaleToWidth(width:CGFloat)->UIImage {
    //获得图片高度
    let height = width / self.size.width  * self.size.height
    //绘制区域
    let rect :CGRect = CGRect(x: 0, y: 0, width: width, height: height)
    //1.开启图形上下文
    UIGraphicsBeginImageContext(rect.size)
    //2.把图片绘制到区域
    self.drawInRect(rect)
    //3.取出图片
    let image = UIGraphicsGetImageFromCurrentImageContext()
    //4.关闭图形上下文
    UIGraphicsEndImageContext()
    //5.返回图片
    return image
    }
}
